package IOPractice;

import IO.Buffered;

import java.io.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author wuhan
 * @date 2019/10/15 19:31
 */
class Student implements Comparable<Student>{
    private String name;
    private int ma,cn,en;
    private int sum;

    public Student(String name, int ma, int cn, int en) {
        this.name = name;
        this.ma = ma;
        this.cn = cn;
        this.en = en;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", ma=" + ma +
                ", cn=" + cn +
                ", en=" + en +
                ", sum=" + sum +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMa() {
        return ma;
    }

    public void setMa(int ma) {
        this.ma = ma;
    }

    public int getCn() {
        return cn;
    }

    public void setCn(int cn) {
        this.cn = cn;
    }

    public int getEn() {
        return en;
    }

    public void setEn(int en) {
        this.en = en;
    }

    public int getSum() {
        return this.en+this.ma+this.cn;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    @Override
    public int compareTo(Student o) {
        int num= Integer.compare(this.sum, o.sum);
        if (num==0)
            return this.name.compareTo(o.name);
        return num;
    }
    public int hashCode(){
        return name.hashCode()+sum*78;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return sum == student.sum &&
                name.equals(student.name);
    }
}
class StudentInfoTool{
    public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException {
        BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        Set<Student> stus = null;
        if (cmp==null)
            stus=new TreeSet<Student>();
        else
            stus = new TreeSet<Student>(cmp);
        while ((line=bufferedReader.readLine())!=null){
            if ("over".equals(line))
                break;
            String[] info = line.split(",");
            Student stu = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
            stus.add(stu);
        }
        bufferedReader.close();
        return stus;
    }

    public static Set<Student> getStudents() throws IOException {
       return getStudents(null);
    }

    public static void write2File(Set<Student> stus) throws IOException {
        BufferedWriter bufw = new BufferedWriter(new FileWriter("stusInfo.txt"));
        for (Student student : stus) {
            bufw.write(student.toString()+"\t");
            bufw.write(student.getSum()+""); bufw.newLine();
            bufw.flush();
        }
        bufw.close();
    }




}

class StudentInfoTest{
    public static void main(String[] args) throws IOException {
//        Comparator<Student> comparator= Collections.reverseOrder();
        Set<Student> students = StudentInfoTool.getStudents();
        StudentInfoTool.write2File(students);
    }
}
